package com.xhis.system.print.web.actions;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Blob;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.context.annotation.Scope;

import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.SessionScope;
import net.sourceforge.stripes.action.StreamingResolution;
import net.sourceforge.stripes.integration.spring.SpringBean;

import com.xhis.system.print.service.PrintService;
import com.xhis.system.print.web.AbstractActionBean;


public class ImageAction extends AbstractActionBean {

	@SpringBean
	private transient PrintService printService;
	private transient String fid;
	private transient String sign;
    private String time;
	public String getTime() {
		return time;
	}
	public void setTime(String time) {
		this.time = time;
	}
	@DefaultHandler
	public Resolution image() throws Exception {
		return new StreamingResolution("text/plain") {
			public void stream(final HttpServletResponse response)
					throws Exception {
				InputStream sample = null;
				response.setHeader("Cache-Control","no-cache"); //HTTP 1.1    
				response.setHeader("Pragma","no-cache"); //HTTP 1.0    
				response.setDateHeader ("Expires", 0); //prevents caching at the proxy server   
				try {
					// sample = getResourceAsStream(SAMPLE
					// + getContext().getLocale().toString());
//					response.setHeader("Cache-Control","no-cache"); //HTTP 1.1    
//					response.setHeader("Pragma","no-cache"); //HTTP 1.0    
//					response.setDateHeader ("Expires", 0); //prevents caching at the proxy server  
					Map printInfo = printService.getPrintInfo(fid);
					Blob b = (Blob)printInfo.get(returnSignPersion(sign));
					if(b==null) return;
					sample = b.getBinaryStream();
					//if (sample == null)
					// sample = getResourceAsStream(SAMPLE + "en_US");

					final OutputStream out = response.getOutputStream();
					final byte buffer[] = new byte[8192];

					// out.write((HEADER + "\n").getBytes());

					for (int rc = sample.read(buffer); rc > 0; rc = sample
							.read(buffer))
						out.write(buffer, 0, rc);
				} finally {
					if (sample != null)
						try {
							sample.close();
						} catch (IOException ioe) {
							ioe.printStackTrace();
						}
				}
			}
		};
	}
	public void clear(){
		fid = null;
		sign = null;
	}
	
	private String returnSignPersion(String sign) {
		if (sign == null || "".equals(sign)) {
			return null;
		}
		String signPerson = null;
		int i = Integer.parseInt(sign);
		switch (i) {
		case 1:
			signPerson = "FEXTENDDATE1";// 主管
			break;
		case 2:
			signPerson = "FEXTENDDATE2";// 会计
			break;
		case 3:
			signPerson = "FEXTENDDATE3";// 出纳
			break;
		case 4:
			signPerson = "FEXTENDDATE4";// 财务总监
			break;
		case 5:
			signPerson = "FEXTENDDATE5";// 收款人
			break;
		case 6:
			signPerson = "FAPPLYPERSON";// 报账人
			break;

		default:
			signPerson = null;
		}
		return signPerson;

	}

	public String getFid() {
		return fid;
	}

	public void setFid(String fid) {
		this.fid = fid;
	}

	public String getSign() {
		return sign;
	}

	public void setSign(String sign) {
		this.sign = sign;
	}
}
